package cn.anc.aonicardv.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.anc.aonicardv.MyApplication;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.util.ui.UIUtil;


/**
 * 通用条目布局.
 * <p/>
 * 包括：1.左边图片，2.左边文字，3.右边文字，4.右边箭头,5.右边开关；eg:设置界面
 * </p>
 * Created by llc on 2018/11/2.
 */
public class CommonItemLayout extends LinearLayout {
    private View mLayoutRoot;
    private TextView mLeftTxt, mRightTxt;
    private ImageView mRightImgArrow;
    /** 默认条目的高度 120 */
    private static final int DEFAULT_ITEM_HEIGHT = UIUtil.dip2px(MyApplication.getInstance(),120);
    private OnClickListener itemClickListener;

    public CommonItemLayout(Context context) {
        this(context, null);
    }

    public CommonItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    /**
     * 初始化设置组合控件的界面
     */
    @SuppressWarnings({"deprecation", "ResourceType"})
    private void initView(Context context, AttributeSet attrs) {
        this.setOrientation(VERTICAL);
        View v = View.inflate(context, R.layout.layout_view_common_item, null);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        this.addView(v, params);
        mLayoutRoot = v.findViewById(R.id.layout_view_common_item_root);
        this.setBackgroundColor(getResources().getColor(R.color.common_item_n));
        ImageView mLeftImg = (ImageView) v.findViewById(R.id.layout_view_common_item_left_iv);
        mLeftTxt = (TextView) v.findViewById(R.id.layout_view_common_item_left_tv);
        mRightTxt = (TextView) v.findViewById(R.id.layout_view_common_item_right_tv);
         mRightImgArrow = (ImageView) v.findViewById(R.id.layout_view_common_item_right_iv);

        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonItemLayout);

            Drawable leftImg = ta.getDrawable(R.styleable.CommonItemLayout_leftImage);
            String leftTxt = ta.getString(R.styleable.CommonItemLayout_leftText);
            String rightTxt = ta.getString(R.styleable.CommonItemLayout_rightText);

            //左边图片
            if (null != leftImg) {
                mLeftImg.setVisibility(VISIBLE);

                mLeftImg.setBackgroundDrawable(leftImg);
            } else {
                mLeftImg.setVisibility(GONE);
            }
            //左边文字
            if (null != leftTxt) {
                mLeftTxt.setVisibility(VISIBLE);
                mLeftTxt.setText(leftTxt);
            }
            //右边文字
            if (null != rightTxt) {
                mRightTxt.setVisibility(VISIBLE);
                mRightTxt.setText(rightTxt);
            }

            //条目点击效果
            this.setClickable(true);
            this.setBackgroundResource(R.drawable.selector_color_bg_common_item);
            this.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onClick(v);
                    }
                }
            });
            ta.recycle();
        }
    }


    /**
     * 条目点击监听器
     */
    public void setOnItemClickListener(OnClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public void setItemBackgroundResource(int resid) {
        mLayoutRoot.setBackgroundResource(resid);
    }

    public void setItemBackgroundColor(int color) {
        mLayoutRoot.setBackgroundColor(color);
    }

    public void setLeftText(CharSequence text) {
        mLeftTxt.setText(text);
    }

    public void setLeftText(int resid) {
        mLeftTxt.setText(resid);
    }

    public void setLeftTextSize(float size) {
        mLeftTxt.setTextSize(size);
    }

    public void setLeftTextSize(int unit, float size) {
        mLeftTxt.setTextSize(unit, size);
    }

    public void setLeftTextColor(int color) {
        mLeftTxt.setTextColor(color);
    }

    public void setRightText(CharSequence text) {
        mRightTxt.setText(text);
    }
    public String getRightText() {
       return mRightTxt.getText().toString();
    }
    public void setRightText(int resid) {
        mRightTxt.setText(resid);
    }

    public void setRightTextSize(float size) {
        mRightTxt.setTextSize(size);
    }

    public void setRightTextSize(int unit, float size) {
        mRightTxt.setTextSize(unit, size);
    }

    public void setRightTextColor(int color) {
        mRightTxt.setTextColor(color);
    }

    public void setRightTextBackgroundResource(int resId) {
        mRightTxt.setBackgroundResource(resId);
    }

    @SuppressWarnings("deprecation")
    public void setRightTextBackgroundDrawble(Drawable drawable) {
        mRightTxt.setBackgroundDrawable(drawable);
    }

    public void setRightTextVisibility(int visibility) {
        mRightTxt.setVisibility(visibility);
    }
    public void setRightImgMoreVisibility(int visibility) {
        mRightImgArrow.setVisibility(visibility);
    }
}
